package chabernac.android.wordraster.rastersolver;

import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;

import chabernac.android.wordraster.model.WordRaster;

public class WordRasterFactory {
  private static Logger LOGGER = Logger.getLogger(WordRasterFactory.class.getName());
  private static Random myRandom = new Random();
  public static final String VOCALS = "AEIOU";
  public static final String NON_VOCALS = "BCDFGHJKLMNPQRSTVWXYZ";
  
  private final iStringDatabase myStringDatabase;
  
  public WordRasterFactory(iStringDatabase aStringDatabase){
    myStringDatabase = aStringDatabase;
  }

  public WordRaster createRaster(String aLanguage, int aRows, int aColumns){
    WordRaster theRaster = new WordRaster( aLanguage, aRows, aColumns);
    populateRasterRandomly( theRaster, aLanguage );
    return theRaster;
  }
 
  private void populateRasterRandomly(WordRaster aRaster, String aLanguage){
    try{
      RandomLetterHelper theRandomLetterHelper = myStringDatabase.getRandomLetterHelper(aLanguage);
      for(int row = 0; row < aRaster.getRows(); row++){
        for(int column = 0; column < aRaster.getColumns(); column ++){
          aRaster.setLetter(row, column, theRandomLetterHelper.getRandomLetter());
        }
      }
    }catch(Exception e){
      LOGGER.log( Level.SEVERE, "Error occured when populating raster", e );
    }
  }

  private static void populateRasterRandomly(WordRaster aRaster) {
    for(int row = 0; row < aRaster.getRows(); row++){
      for(int column = 0; column < aRaster.getColumns(); column ++){
        aRaster.setLetter(row, column, (char)('A' + Math.abs(myRandom.nextInt()) % 26));
      }
    }
  }
}
